package com.study.chunguard.common.enums;

import com.study.chunguard.common.core.ArrayValuable;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Arrays;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 用户执法类型：0-中心调度员；1-指挥员；2-办件员
 *
 * @author zhaowenhao
 * @since 2023-10-19
 */
@Getter
@AllArgsConstructor
public enum LawTypeEnum implements ArrayValuable {
    /**
     * 中心调度员
     */
    CENTER_DISPATCHER("0", "中心调度员"),

    /**
     * 指挥员
     */
    COMMANDER("1", "指挥员"),

    /**
     * 办件员
     */
    CLERK("2", "办件员");

    /**
     * 代码
     */
    private final String code;

    /**
     * 名称
     */
    private final String name;

    private static final Map<String, LawTypeEnum> CODE_TO_ENUM = Arrays.stream(LawTypeEnum.values())
            .collect(Collectors.toMap(LawTypeEnum::getCode, Function.identity()));

    private static final Map<String, LawTypeEnum> NAME_TO_ENUM = Arrays.stream(LawTypeEnum.values())
            .collect(Collectors.toMap(LawTypeEnum::getName, Function.identity()));

    public static LawTypeEnum of(String code) {
        return Optional.ofNullable(CODE_TO_ENUM.get(code))
                .orElseThrow(() -> new IllegalArgumentException("用户执法类型错误"));
    }

    public static String getCodeByName(String name) {
        return Optional.ofNullable(NAME_TO_ENUM.get(name))
                .map(LawTypeEnum::getCode)
                .orElseThrow(() -> new IllegalArgumentException("用户执法类型错误"));
    }

    @Override
    public String[] array() {
        return Arrays.stream(values()).map(LawTypeEnum::getCode).toArray(String[]::new);
    }
}
